import json
import threading
import time

import requests as requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys

from config import webdriverPath, hostUrl
from wos.test import parse_html
import threading


# pip install
# import PyExecJS
class WOS_spider:
    i = 0
    item = None

    def set(self, item):
        self.i = item['currQuantity']
        self.list = []
        self.item = item
        self.citationList = []

    def run(self):
        # url_root = 'https://webofscience-clarivate-cn-s.era.lib.swjtu.edu.cn/wos/alldb/basic-search'
        # 创建ChromeOptions对象
        chrome_options = webdriver.ChromeOptions()
        # 禁止加载图片等资源
        chrome_options.add_argument("--disable-images")
        chrome_options.add_argument("--disable-plugins")
        chrome_options.add_argument("--disable-extensions")
        # 设置全屏参数
        chrome_options.add_argument("--start-maximized")

        # 创建WebDriver对象时传入ChromeOptions
        self.driver = webdriver.Chrome(options=chrome_options, service=Service(webdriverPath))
        self.driver.get('http://www.scihuber.com/e/member/login/')  # 打开的页面

        # # 登录
        # WebDriverWait(driver, wait_time).until(
        #     EC.visibility_of_element_located((By.ID, 'username'))
        # )846005300697,496659
        self.driver.find_element(By.ID, 'username').send_keys('354098')
        self.driver.find_element(By.ID, 'password').send_keys('05858298118')
        self.driver.find_element(By.CSS_SELECTOR, '#maincolumn .lBtn').click()
        # input("请手动操作至论文详情页面,完成后按Enter键继续...")
        time.sleep(5)

    def swlang(self):

        self.driver.find_element(By.CSS_SELECTOR, 'body > app-wos > main > div > div > div.ng-star-inserted > app-header > div.with-header > header > div.cdx-header__global-bar > cdx-header-global > button:nth-child(7)').click()

        time.sleep(1)

        self.driver.find_element(By.CSS_SELECTOR, '#mat-menu-panel-1 > div > div > button:nth-child(5)').click()

    def closeCookie(self):

        # 关闭管理Cookie栏
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, '#onetrust-close-btn-container > button'))
        )
        time.sleep(5)
        self.driver.find_element(By.CSS_SELECTOR, '#onetrust-close-btn-container > button').click()
        try:
            self.driver.find_element(By.CSS_SELECTOR, '._pendo-close-guide').click()
            time.sleep(2)
            self.driver.find_element(By.CSS_SELECTOR, '._pendo-close-guide').click()
        except:
            print("")

    def closeCookie1(self):

        # 关闭管理Cookie栏
        # WebDriverWait(self.driver, 10).until(
        #     EC.visibility_of_element_located((By.CSS_SELECTOR, '#onetrust-close-btn-container > button'))
        # )
        # time.sleep(5)
        try:
            self.driver.find_element(By.CSS_SELECTOR, '#onetrust-close-btn-container > button').click()
            self.driver.find_element(By.CSS_SELECTOR, '._pendo-close-guide').click()
            # time.sleep(2)
            self.driver.find_element(By.CSS_SELECTOR, '._pendo-close-guide').click()
        except:
            print("")
    def search(self):
        # 切换url https://www.webofscience.com/wos/alldb/basic-search
        # driver.href =
        # driver.get('https://www.webofscience.com/wos/alldb/basic-search')
        # self.driver.get('https://webofscience.clarivate.cn/wos/alldb/basic-search')
        # 搜索 准备爬数据
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CLASS_NAME, 'selects'))
        )
        self.driver.find_element(By.CLASS_NAME, 'selects').click()
        # 切换选项 出版物/来源出版物名称
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#global-select > div.options-and-search > div > div[title="Publication/Source Titles"]').click()

        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'search-option'))
        )
        # 输入搜索内容 清空内容后输入
        search_option = self.driver.find_element(By.ID, 'search-option')
        search_option.clear()
        search_option.send_keys(self.item['title'])
        # 失去焦点
        webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
        # 点击搜索
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#snSearchType > div.button-row > button.mat-focus-indicator.search.mat-flat-button.mat-button-base.mat-primary').click()
        # 判断是否搜索成功
        time.sleep(0.2)
        error_code = self.driver.find_elements(By.CSS_SELECTOR,
                                               '#snSearchType > div.search-error.error-code.light-red-bg.ng-star-inserted')
        if len(error_code) > 0:
            print("搜索失败")
            return

        try:

            WebDriverWait(self.driver, 5).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR, '#pendo-base ._pendo-close-guide'))
            )
            # 或者等待直到某个元素可见
            self.driver.find_element(By.CSS_SELECTOR,
                                     '#pendo-base ._pendo-close-guide').click()
        except:
            print("")
        self.click0()

    def click0(self):
        self.scroll()
        # 获取搜索数量 然后点击列表进入详情页面开始爬数据
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
                                              'app-base-summary-component app-records-list > app-record app-summary-title a:nth-child(1)'))
        )
        # 点击第一个
        self.driver.find_element(By.CSS_SELECTOR,
                                 'app-base-summary-component app-records-list > app-record app-summary-title a:nth-child(1)').click()
        self.skip()

    def scroll(self):
        scrollHeight = self.driver.execute_script("return document.body.scrollHeight")
        scroll = 500
        # 滑动页面直到找到元素
        while scroll < scrollHeight + 5000:
            # 滑动到页面底部
            self.driver.execute_script("window.scrollTo(0, " + str(scroll) + ");")
            scroll = scroll + 500

            time.sleep(0.4)

    def skip(self):
        # 跳转
        if self.i > 0:
            time.sleep(5)
            nextPage = self.driver.find_element(By.CSS_SELECTOR, '#snNextPageTop')
            nextPage.clear()
            nextPage.send_keys(self.i)
            nextPage.send_keys(Keys.RETURN)  # 模拟按下回车键
        else:
            self.i = 0

    def get_qikan_data(self):
        condition = True
        while condition and self.i < 10000000000:
            # 等待页面加载
            # 或者等待直到某个元素可见
            WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                  'app-page-controls .pagination > div > button:nth-child(4)'))
            )
            time.sleep(1)

            url = self.driver.current_url

            # 解析HTML
            html = self.driver.page_source
            index, data = parse_html(html)
            # print(data)
            print(self.item['title'], "：正在处理第", index, "篇论文")

            # 类型来源
            data['type'] = self.item['type']
            data['sourceWebsite'] = 'wos'
            data['language'] = 'en'
            data['datasource'] = self.item['title']
            data['database'] = '期刊'
            # data['infoData'] = item['infoData']
            # 获取当前url
            data['url'] = url

            res = requests.post(hostUrl + '/literature/getByAccessionNo',
                                json={'accessionNo': data['accessionNo']})
            if res.json().get('data') == '0':
                print(self.item['title'], "保存")
                requests.post(hostUrl + '/literature/wosSave', json=data)
            else:
                print(self.item['title'], "该论文已存在")

            # old_url = self.driver.current_url
            # 手动操作，比如切换标签页等
            nextBtn = self.driver.find_element(By.CSS_SELECTOR,
                                               'app-page-controls .pagination > div > button:nth-child(4)')
            if 'mat-button-disabled' not in nextBtn.get_attribute('class'):
                # 切换到下一页
                nextBtn.click()
            # input("请手动操作至论文详情页面,完成后按Enter键继续...")
            else:
                condition = False
                self.item['status'] = 2

            self.i = self.i + 1

# 创建递归锁
rlock = threading.RLock()

def exec_Wos(item):
    if item['status'] == '2':
        return
    index = 0
    i =1
    isError = True
    while isError:
        isError = False
        a = None
        try:
            a = WOS_spider()
            with rlock:
                print("线程", item['title'], "开始")
                a.run()
            # 放到外面提高登录效率
            a.driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=4383')  # 打开的页面
            # a.driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=3023')  # 打开的页面

            time.sleep(5)
            a.closeCookie()
            # a.setCookie()
            # 类型 10搜索 20 引用
            if item['type'] == '10':
                a.search()
            else:
                a.driver.get(item['citationurl'])
                a.click0()

            a.get_qikan_data()
            a.driver.quit()

            requests.put(hostUrl + '/wosListTemp/put',
                         params={'id': item['id'], 'currQuantity': a.i, 'status': a.item['status']})

        except Exception as e:
            print(e)
            isError = True
            index = index - 1
            if i > 3:
                break
            i +=1
            a.item['currQuantity'] = a.i
            requests.put(hostUrl + '/wosListTemp/put',
                         params={'id': item['id'], 'currQuantity': a.i})
            a.driver.quit()
        i = 1

def arrThread():

    index = 0
    i =1
    isError = True
    while isError:
        isError = False
        a = None
        try:
            a = WOS_spider()
            with rlock:
                print("线程", "开始")
                a.run()
            # 放到外面提高登录效率
            # a.driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=4383')  # 打开的页面
            # a.driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=3023')  # 打开的页面
            a.driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=3251')  # 打开的页面
            # a.driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=4210')  # 打开的页面
            time.sleep(5)
            a.closeCookie()
            a.swlang()

            b = True
            while b:
                res = requests.get('http://localhost:1728/qikan/wosListTemp/getWosTemp')

                if res.json()['success']:
                    item = res.json()['data'][0]
                    if item['status'] == '2':
                        continue
                    a.set(item)
                    if item['type'] == '10':
                        a.search()
                    else:
                        a.driver.get(item['citationurl'])
                        a.click0()

                    a.get_qikan_data()
                    requests.put(hostUrl + '/wosListTemp/put',
                                 params={'id': item['id'], 'currQuantity': a.i, 'status': a.item['status']})
                else:
                    b = False
            a.driver.quit()


        except Exception as e:
            print(e)
            isError = True
            index = index - 1
            if i > 3:
                break
            i +=1
            if a.item is not None:
                a.item['currQuantity'] = a.i
                requests.put(hostUrl + '/wosListTemp/put',
                             params={'id': a.item['id'], 'currQuantity': a.i})

            if a is not None:
                a.driver.quit()
        i = 1

#   获取明细数据
if __name__ == "__main__":
    res = requests.get('http://localhost:1728/qikan/wosListTemp/setPage?initialValue=6300')
    for i in range(0, 7):
        thread = threading.Thread(target=arrThread)
        # threads.append(thread)
        thread.start()  # 启动线程
        time.sleep(20)
